package cn.quevo.core.lang;

/**
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018-03-09 16:19:51
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 * @return
 */
public class StringFormat {

    /**
     * 格式化属性
     *
     * @param property
     * @param firstUp
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:23:46
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    public static String formatProperty(String property, boolean firstUp) {
        if (StringUtils.isBlank(property)) {
            return property;
        }
        String[] args = property.split("_");
        StringBuffer str = new StringBuffer(firstLetter(args[0], firstUp));
        if (args.length > 1) {
            for (int i = 1; i < args.length; i++) {
                str.append(firstLetter(args[i], true));
            }
        }
        return str.toString();
    }

    /**
     * 首字母大写/小写
     *
     * @param str
     * @param firstUp
     * @return
     * @author July july_sky@foxmail.com
     * @date 2018-03-09 16:23:53
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    private static String firstLetter(String str, boolean firstUp) {
        if (str.length() == 1) {
            return firstUp ? str.toUpperCase() : str.toLowerCase();
        }
        return (firstUp ? str.substring(0, 1).toUpperCase() : str.substring(0, 1).toLowerCase()).concat(str.substring(1));
    }
}
